<template>
  <div class="move-robot">
    <div class="gutter-box">
      <div class="row-title">
        <div class="left-title">机器人运动轨迹</div>
      </div>

      <div class="content-msg">
        <div class="all-board">
          <div class="one-robot" v-for="(item, index) in robotList">
            <div class="grid-position" :style="getStyes(item)">
              <div v-for="i in item.totalNum" class="one-board"></div>
              <div
                class="robot-model"
                :class="[
                  index != 0 ? 'animation80' : 'animation50',
                  item.animationStatus ? 'animationRun' : 'animationPaused',
                  item.reset ? 'reset' : '',
                ]"
              >
                <div class="inner-box"></div>
              </div>
            </div>
            <div class="operation-btn">
              <div @click="robotStatu(1, item)">启动</div>
              <div @click="robotStatu(0, item)">停止</div>
              <div @click="resetRobot(item)">复位</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  robotParamList,
  robotRun,
  resettingRobot,
  robotStop,
} from "@/api/device.js";
export default {
  data() {
    return {
      robotList: [],
    };
  },
  methods: {
    getRobotParam() {
      robotParamList().then((res) => {
        console.log(res);
        res.data.forEach((item) => {
          item.totalNum = item.groupNumber * item.boardNumber;
          item.animationStatus = false;
          item.reset = false;
        });
        this.robotList = res.data;
      });
    },
    robotStatu(flag, item) {
      // const myElement = this.$refs["model" + id];
      if (flag) {
        robotRun({ id: item.robotId }).then((res) => {
          if (res.code == "000000") {
            item.animationStatus = true;
            item.reset = false;
            this.$message.success(`启动成功`);
            // this.getTableList();
          } else {
            this.$message.error(`启动失败`);
          }
        });
      } else {
        robotStop({ id: item.robotId }).then((res) => {
          if (res.code == "000000") {
            item.animationStatus = false;
            this.$message.success(`关闭成功`);
            // this.getTableList();
          } else {
            this.$message.error(`关闭失败`);
          }
        });
      }
    },
    resetRobot(item) {
      resettingRobot({ id: item.robotId }).then((res) => {
        if (res.code == "000000") {
          item.animationStatus = false;
          item.reset = true;
          this.$message.success(`复位成功`);
          // this.getTableList();
        } else {
          this.$message.error(`复位失败`);
        }
      });
    },
    getStyes(item) {
      return {
        gridTemplateRows: `repeat(${item.groupNumber},1.125rem)`,
        gridTemplateColumns: `repeat(${item.boardNumber}, 0.75rem)`,
      };
    },
  },
  mounted() {
    this.getRobotParam();
  },
};
</script>
<style lang="less" scoped>
.move-robot {
  padding: 16px;
  box-sizing: border-box;
  height: 100%;
  overflow-y: auto;
  .gutter-box {
    // height: 240px;
    background: #fff;
    margin-bottom: 24px;
    padding: 16px;
    box-sizing: border-box;
    box-shadow: 0 5px 10px 0 rgba(4, 14, 31, 0.2);
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    .content-msg {
      .all-board {
        margin-top: 20px;
        padding-left: 40px;
        .one-robot {
          display: flex;
          align-items: center;
          margin-bottom: 20px;
          .grid-position {
            display: grid;
            grid-template-rows: 90px 90px;
            grid-template-columns: repeat(8, 60px);
            column-gap: 10px;
            row-gap: 5px;
            position: relative;
            width: max-content;
            .one-board {
              background: #151b4b;
            }
            @keyframes robotMove {
              0% {
                right: 0;
              }
              50% {
                right: calc(100% - 30px);
              }
              100% {
                right: 0;
              }
            }
            .robot-model {
              height: calc(100% - 10px);
              width: 30px;
              background: #fff;
              box-shadow: 0 0 15px rgba(4, 14, 31, 0.6);
              position: absolute;
              right: 0;
              padding: 5px;
              box-sizing: border-box;

              // transform: translate(100%);
              top: 0;
              .inner-box {
                background: #03a0d3;
                height: calc(50% + 15px);
              }
            }
            .animation50 {
              animation: robotMove 50s linear alternate;
            }
            .animation80 {
              animation: robotMove 80s linear alternate;
            }
            .animationRun {
              animation-play-state: running;
            }
            .animationPaused {
              animation-play-state: paused;
            }
            .reset {
              position: absolute;
              right: 0 !important;
            }
          }
          .operation-btn {
            display: flex;
            margin-left: 30px;
            div {
              margin-right: 24px;
              padding: 0 12px;
              font-size: 16px;
              cursor: pointer;
              color: #048fff;
            }
          }
        }
      }
    }
  }
}
</style>